package com.fu.web.serviceImplments;

import com.fu.web.DTO.*;
import com.fu.web.entity.*;
import com.fu.web.mapper.*;
import com.fu.web.service.docVueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.plaf.synth.SynthSpinnerUI;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SortedMap;

@Service
public class docVueServiceImpl implements docVueService {
    @Autowired
    private com.fu.web.mapper.patientMapper patientMapper;
    @Autowired
    private com.fu.web.mapper.descbookMapper descbookMapper;
    @Autowired
    private prescriptionMapper mapper;
    @Autowired
    private drugMapper mapper02;
    @Autowired
    private chargeFormMapper chargeFormMapper;

    @Override
    public List<unOveredpatient> showUn() {
        List<unOveredpatient> list=new ArrayList<unOveredpatient>();
        List<descbook> descbooks = descbookMapper.selectAll();

        List<patient> patients = patientMapper.selectALl();
        unOveredpatient p1=null;
        for (descbook descbook : descbooks) {
            if(descbook.getIsOver()==0) {
                for (patient patient : patients) {
                    if (descbook.getPatiid().equals(patient.getPatientId())) {
                        p1 = new unOveredpatient();
                        p1.setPatientName(patient.getPatientName());
                        p1.setRegisterfromId(descbook.getRegisterformId());
                        list.add(p1);
                    }
                }
            }

        }
        return list;
    }

    @Override
    public List<overedPatient> showEN() {
        List<overedPatient> list=new ArrayList<overedPatient>();
        List<descbook> descbooks = descbookMapper.selectAll();
        List<patient> patients = patientMapper.selectALl();
        overedPatient p1;
        for (descbook descbook : descbooks) {
            if(descbook.getIsOver()==1) {
                for (patient patient : patients) {
                    if (descbook.getPatiid().equals(patient.getPatientId())) {
                        p1 = new overedPatient();
                        p1.setPatientName(patient.getPatientName());
                        p1.setRegisterfromId(descbook.getRegisterformId());
                        list.add(p1);

                    }
                }
            }

        }

        return list;

    }

    @Override
    public descBookVue processDescbook(descBookVue descBookVue) {
        com.fu.web.entity.descbook select = descbookMapper.selectByRegistetId(descBookVue.getDescbookId());
        select.setDesc(descBookVue.getBodyDesc());
        select.setPastHis(descBookVue.getPastHis());
        select.setAllergyHis(descBookVue.getAllergyHis());
        select.setPresId(descBookVue.getPreIds());
        select.setIsOver(1);


        descbookMapper.updateByPrimaryKey(select);
        chargeForm form=new chargeForm();
        List<patient> patients = patientMapper.selectALl();
        String Pname="";
        for (patient patient : patients) {
            if(patient.getPatientId().equals(select.getPatiid())){
                Pname=patient.getPatientName();
                break;
            }
        }
        List<prescription> prescriptions = mapper.selectAll();
        Integer drugId=new Integer(0);
        for (prescription prescription : prescriptions) {
            if(prescription.getPrescriptionId()==select.getPresId()){
                drugId=prescription.getDurgId();
            }
        }
        BigDecimal totalPrice=new BigDecimal(0);
        List<drug> all = mapper02.all();
        for (drug drug : all) {
            if (drug.getDrugId()==drugId){
                BigDecimal t1=new BigDecimal(String.valueOf(drug.getDrugPrice()));
                BigDecimal t2=new BigDecimal(2);
                totalPrice=t1.multiply(t2);
                break;
            }
        }

        form.setIsover(0);
        form.setIsDelete(0);
        form.setIsGiven(0);
        form.setBookid(select.getBookId());
        form.setStaffId(1);
        form.setChargeId(null);
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date); //2013-01-14
        form.setCreateTime(str);
        form.setPrescriptionId(select.getPresId());
        form.setChargepatientId(select.getPatiid());
        form.setTotalPrice(totalPrice);
        chargeFormMapper.insert(form);

        return null;

    }

    @Override
    public List<presVue> processPre(){
        List<prescription> prescriptions = mapper.selectAll();
        List<presVue> list=new ArrayList<presVue>();
        List<drug> listDrug=mapper02.all();

        for (prescription prescription : prescriptions) {
            for (drug drug : listDrug) {
                if(drug.getDrugId().equals(prescription.getDurgId())){
                    presVue vue=new presVue();
                    vue.setDrugsName(drug.getDrugName());
                    vue.setPrescriptionDesc(prescription.getPrescriptionDesc());
                    vue.setPrescriptionId(prescription.getPrescriptionId());
                    list.add(vue);
                    break;
                }

            }



        }

        return list;


    }

    @Override
    public List<showDrug> showDrug() {
        List<drug> all = mapper02.all();
        List<showDrug> list=new ArrayList<showDrug>();
        for (drug drug : all) {
            showDrug showDru=new showDrug();
            showDru.setDrugId(drug.getDrugId());
            showDru.setDrugName(drug.getDrugName());
            list.add(showDru);
        }
        return list;
    }

    @Override
    public void addPre(addPreVue addPreVue) {
        String[] list=new String[2];
        list=addPreVue.getDrug_and_num().split(",");
        prescription prescription=new prescription();
        int num=Integer.parseInt(list[1]);
        prescription.setDrugNum(num);
        prescription.setPrescriptionDesc(addPreVue.getDesc());
        List<drug> all = mapper02.all();
        for (drug drug : all) {
            if(drug.getDrugName().equals(list[0])){
                prescription.setDurgId(drug.getDrugId());
                break;
            }
        }
       mapper.insert(prescription);
    }
}
